/* **************************************************
 * User Group Manager
 *      author:     Baskin Tapkan - baskint@hotmail.com
 *          written for Code Plex
 *              April - 2008
 *                  http://tapkan.com
 * ****************************************************
 */

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace UserGroupManager
{
    static class Utility
    {
        // borrowed from :http://codeedoc.com/c/sorting-data-table-in-c/
        public static DataSet SortDataTable(DataSet ds, string sortColumn)
        {
            DataTable tempDataTable = ds.Tables[0].Clone();
            int rowCount = ds.Tables[0].Rows.Count;
            int columnCount = ds.Tables[0].Columns.Count;
            DataRow[] allRows = ds.Tables[0].Select(null, sortColumn); // Sorting with column name.

            for (int i = 0; i < rowCount; i++)
            {
                object[] rowArray = new object[columnCount];
                for (int j = 0; j < columnCount; j++)
                {
                    rowArray[j] = allRows[i][j];
                }
                DataRow tempRow = tempDataTable.NewRow();
                tempRow.ItemArray = rowArray;
                tempDataTable.Rows.Add(tempRow);
            }

            ds.Tables[0].Rows.Clear();
            for (int i = 0; i < tempDataTable.Rows.Count; i++)
            {
                object[] rowArray = new object[columnCount];
                for (int j = 0; j < columnCount; j++)
                {
                    rowArray[j] = tempDataTable.Rows[i][j];
                }
                DataRow tempRow = ds.Tables[0].NewRow();
                tempRow.ItemArray = rowArray;
                ds.Tables[0].Rows.Add(tempRow);
            }

            return ds;
        }
    }
}
